package com.kma.summer2012.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.kma.summer2012.client.constants.ConstantsPresenter;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.presenters.authorization.AuthorizationPresenter;
import com.kma.summer2012.client.presenters.authorization.ChangePasswordPresenter;
import com.kma.summer2012.client.presenters.authorization.ConfirmPasswordPresenter;
import com.kma.summer2012.client.presenters.authorization.PasswordRecoveryPresenter;
import com.kma.summer2012.client.presenters.closerequest.CloseRequestPresenter;
import com.kma.summer2012.client.presenters.header.HeaderPresenter;
import com.kma.summer2012.client.presenters.mainPage.MainPagePresenter;
import com.kma.summer2012.client.presenters.manager.*;
import com.kma.summer2012.client.presenters.reject.RejectPresenter;
import com.kma.summer2012.client.presenters.reject.endRejectPresenter;
import com.kma.summer2012.client.presenters.request.RequestPresenter;
import com.kma.summer2012.client.presenters.viewmap.ViewMapPresenter;
import com.kma.summer2012.client.view.authorization.AuthorizationView;
import com.kma.summer2012.client.view.authorization.ChangePasswordView;
import com.kma.summer2012.client.view.authorization.ConfirmPasswordView;
import com.kma.summer2012.client.view.authorization.PasswordRecoveryView;
import com.kma.summer2012.client.view.closeRequest.CloseRequestView;
import com.kma.summer2012.client.view.mainPage.MainPage;
import com.kma.summer2012.client.view.manager.*;
import com.kma.summer2012.client.view.rejectrequest.RejectView;
import com.kma.summer2012.client.view.rejectrequest.endRejectView;
import com.kma.summer2012.client.view.requests.*;
import com.kma.summer2012.client.view.viewmap.ViewMap;

//import com.kma.summer2012.client.presenters.reject.RejectPresenter;
//import com.kma.summer2012.client.presenters.reject.endRejectPresenter;
//import com.kma.summer2012.client.presenters.manager.ManageWorksPresenter;
//import com.kma.summer2012.client.view.manager.ManageWorksView;
//import com.kma.summer2012.client.view.rejectrequest.RejectView;
//import com.kma.summer2012.client.view.rejectrequest.endRejectView;

public class HistoryFactory {
    private static HistoryFactory INSTANCE;
    private static ConstantsPresenter c = GWT.create(ConstantsPresenter.class);

    public static HistoryFactory getInstance() {

        if (INSTANCE == null) {
            INSTANCE = new HistoryFactory();
        }
        return INSTANCE;
    }

    public Presenter goPresenter(String token) {

        if (token != null) {
            Presenter presenter = null;
            if (token.equals(c.LOGIN())) {
                presenter = new AuthorizationPresenter(new AuthorizationView());

            }
            if (token.equals(c.PASSWORD_RECOVERY())) {
                presenter = new PasswordRecoveryPresenter(new PasswordRecoveryView());

            }
            if (token.contains(c.CHANGE_PASSWORD())) {
                if (token.contains("id=")) {
                    presenter = new ChangePasswordPresenter(new ChangePasswordView());
                } else {
                    presenter = new AuthorizationPresenter(new AuthorizationView());
                }
            }
            if (token.equals(c.MANAGE_TOOLS())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new ManageToolsPresenter(new ManageToolsView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.ADD_TOOL())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AddToolPresenter(new AddToolView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.ADD_CATEGORY())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AddWorkCategoryPresenter(new AddWorkCategoryView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.EDIT_CATEGORY())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AddWorkCategoryPresenter(new AddWorkCategoryView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.ADD_WORK())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AddWorkPresenter(new AddWorkView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }

            if (token.equals(c.EDIT_TOOL())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AddToolPresenter(new AddToolView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.EDIT_WORK())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AddWorkPresenter(new AddWorkView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }

            if (token.equals(c.MANAGE_WORKS())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new ManageWorksPresenter(new ManageWorksView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            //temp
            /*if (token.equals("worksCatalog")) {
                if (HeaderPresenter.getUser().getPosition().equals(c.operator())){
                presenter = new WorksCatalogPresenter(new WorksCatalogView());
                }else{
                History.newItem(c.MAIN_PAGE());
                }
            }
                */
            /////////////////////

            if (token.equals(c.CONFIRM_PASSWORD())) {

                presenter = new ConfirmPasswordPresenter(new ConfirmPasswordView());

            }

            if (token.equals(c.SUCCESSFULL_REJECT())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager()) || HeaderPresenter.getUser().getPosition().equals(c.engineer())) {
                    presenter = new endRejectPresenter(new endRejectView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }

            if (token.equals(c.REJECT())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager()) || HeaderPresenter.getUser().getPosition().equals(c.engineer())) {
                    presenter = new RejectPresenter(new RejectView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }


            if (token.equals(c.ASSIGN_PAGE())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new AssignPagePresenter(new AssignPageView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }

            if (token.equals(c.CLOSE_REQUEST())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.engineer())) {
                    presenter = new CloseRequestPresenter(new CloseRequestView());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }

            if (token.equals(c.VIEW_MAP())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new ViewMapPresenter(new ViewMap());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }

            if (token.equals(c.MAIN_PAGE())) {

                presenter = new MainPagePresenter(new MainPage());

            }

           /* if (token.equals("testPage")) {

                presenter = new TestPresenter(new TestPage());

            }  */


            if (token.equals(c.CREATE_REQUEST())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.operator())) {
                    presenter = new RequestPresenter(new CreateRequest());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.EDIT_REQUEST_OPERATOR())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.operator())) {
                    presenter = new RequestPresenter(new EditRequestForOperator());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }

            }
            if (token.equals(c.EDIT_REQUEST_MANAGER())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new RequestPresenter(new EditRequestForManager());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.SHOW_OPERATOR_REQUEST())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.operator())) {
                    presenter = new RequestPresenter(new ShowOperator());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.SHOW_MANAGER_REQUEST())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.manager())) {
                    presenter = new RequestPresenter(new ShowManager(), true);
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (token.equals(c.SHOW_ENGINEER_REQUEST())) {
                if (HeaderPresenter.getUser().getPosition().equals(c.engineer())) {
                    presenter = new RequestPresenter(new ShowEngineer());
                } else {
                    History.newItem(c.MAIN_PAGE());
                }
            }
            if (presenter == null) {
                if (Cookies.getCookie(c.USER()) == null) {
                    presenter = new AuthorizationPresenter(new AuthorizationView());
                } else {
                    return new MainPagePresenter(new MainPage());
                }
            }
            return presenter;
        } else if (Cookies.getCookie(c.USER()) == null) {
            return new AuthorizationPresenter(new AuthorizationView());
        } else {
            return new MainPagePresenter(new MainPage());
        }


    }
}